package com.sunchis.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.sunchis.adapter.MyAdapter;
import com.sunchis.util.MyHttpClientManager;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	public List<String> imageUrls = new ArrayList<String>();
	
	private Gallery gallery;
	private int positon = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        gallery = (Gallery) findViewById(R.id.galleryWall);
    	
    	imageUrls = new ArrayList<String>();
    	imageUrls.add("http://fujian.xabbs.com/forum/201109/02/160646nn9hjjiimixvkxhe.jpg");  
    	imageUrls.add("http://img1.cache.netease.com/catchpic/A/A9/A9D98040B397C366AE93E67871346561.jpg");
    	imageUrls.add("http://new.aliyiyao.com/UpFiles/Image/2011/01/13/nc_129393721364387442.jpg");
    	imageUrls.add("http://comic.sinaimg.cn/2011/0824/U5237P1157DT20110824161051.jpg");
    	
    	MyAdapter adapter = new MyAdapter(MainActivity.this, gallery, imageUrls);
    	gallery.setAdapter(adapter);
    	
    	LinearLayout currentPosition = (LinearLayout) findViewById(R.id.gallery_point_linear);
    	currentPosition.setBackgroundColor(Color.argb(200, 135, 135, 152));
    	for (int i = 0; i < imageUrls.size(); i++) {
    		ImageView pointView = new ImageView(this);
        	if(i==0){
        		pointView.setBackgroundResource(R.drawable.feature_point_cur);
        	} else {
        		pointView.setBackgroundResource(R.drawable.feature_point);
        	}	
        	currentPosition.addView(pointView);
		}
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	init();
    }
    
    private void init() {
    	// 初始化图片缓存目录
    	File cacheDir = new File(MyHttpClientManager.cacheDir);
    	if (!cacheDir.exists()) cacheDir.mkdir();
    }
    
    public void changePointView(int cur){
    	LinearLayout pointLinear = (LinearLayout) findViewById(R.id.gallery_point_linear);
    	View view = pointLinear.getChildAt(positon);
    	View curView = pointLinear.getChildAt(cur);
    	if(view!=null&& curView!=null){
    		ImageView pointView = (ImageView)view;
    		ImageView curPointView = (ImageView)curView;
    		pointView.setBackgroundResource(R.drawable.feature_point);
    		curPointView.setBackgroundResource(R.drawable.feature_point_cur);
    		positon = cur;
    	}
    }
}